package com.content.view.Frame.CBar
{
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.view.Component.CButton.CButtonLevel1;
	import com.content.view.Component.CButton.Level1VO;
	import com.content.view.Component.Toggle.ToggleGroupManager;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;

	public class CBarLevel1 extends UIObject implements IDisposable
	{
		public static const NAME:String							= 'CBarLevel1';		
		public static const CHOOSE_CBUTTON:String					= NAME + "-" + 'ChooseCButton';
		
		private var _bgLevel1:Shape;
		private var _bgBmpData:BitmapData;
		private var _barWidth:int;
		private var _marginLeft:int;
		
		private var _btnList:Array;
		private var _tgManager:ToggleGroupManager;
		
		public function CBarLevel1(barWidth:int, marginLeft:int)
		{
			_barWidth = barWidth;
			_marginLeft = marginLeft;
			
			_btnList = new Array();
			
			AddBG();
			this.addEventListener(MouseEvent.CLICK, OnClick);
		}
		
		private function AddBG():void
		{
			this.alpha = 0;
			this.visible = false;
			
			_bgLevel1 = new Shape();
			addChild(_bgLevel1);
			
			var bg:Sprite = new SkinProxy.IMG_CBar_BG_CBarL1() as Sprite;
			_bgBmpData = new BitmapData(bg.width, bg.height, false);
			_bgBmpData.draw(bg);
		}
		
		/* Event Handler
		* ------------------------------------------------------------- */
		private function OnClick(evt:MouseEvent):void{
			if(evt.target is CButtonLevel1){
				var togItem:CButtonLevel1 = evt.target as CButtonLevel1;
				
				switch(togItem.VO.Category){
					case "Normal":
						_tgManager.ToggleItem(togItem);
						break;
					case "System":
						break;
				}

				dispatchEvent(new CBarL1Event(CHOOSE_CBUTTON, true, false, togItem.VO));
			}
		}
		
		/* Public Method
		* ------------------------------------------------------------- */
		public function Init(cBtnVOList:Array, pSkin:SkinProxy):void{
			var defaultL1Btn:CButtonLevel1;
			_tgManager = new ToggleGroupManager();			
			for each(var l1VO:Level1VO in cBtnVOList){
				var objClass:Class = pSkin.GetClass(l1VO.ICOName);
				var objActiveClass:Class = pSkin.GetClass(l1VO.ICOName + "_Active");
				var l1Btn:CButtonLevel1 = new CButtonLevel1(	new objClass() as Sprite, new objActiveClass() as Sprite, l1VO);
				addChild(l1Btn);
				
				if(l1VO.Default) defaultL1Btn = l1Btn;
				
				if(l1VO.Category == "Normal"){
					_tgManager.AddItem(l1Btn);
				}
				
				_btnList.push(l1Btn);
			}
			
			if(defaultL1Btn == null) return;	// 如果 Config.xml 中没有设置 Default 则返回
			_tgManager.ToggleItem(defaultL1Btn);
			dispatchEvent(new CBarL1Event(CHOOSE_CBUTTON, true, false, defaultL1Btn.VO));
		}
		
		// 留做以后备用
		public function ClearButtons():void{
			var btn:CButtonLevel1;
			while (_btnList.length > 0) {
				btn = _btnList.pop() as CButtonLevel1;
				removeChild(btn);
				btn.Dispose();
			}
		}
		
		public function SetLayout(sHeight:int):void{
			var verticalSpan:int = 10;
			// Redraw the BG
			_bgLevel1.graphics.clear();
			_bgLevel1.graphics.beginBitmapFill(_bgBmpData, null, true);
			_bgLevel1.graphics.drawRect(0,0,_barWidth, sHeight);
			
			// Repos the CButtons
			var cellHeight:int = 52;
			var insertTopY:int = verticalSpan*2;
			var insertBottomY:int = sHeight - cellHeight;
			var item:CButtonLevel1;
			for each(item in _btnList){
				var cBtnWidth:int = int(item.width);
				var cBtnHeight:int = int(item.height);
				var cBtnX:int = int((_barWidth - cBtnWidth)/2);
				item.x = cBtnX;
				
				if(item.VO.Category == "Normal"){					
					item.VO.PosY = insertTopY + (cellHeight+verticalSpan)*item.VO.Order;
				}else if(item.VO.Category == "System"){
					item.VO.PosY = insertBottomY - cellHeight*item.VO.Order;
				}
				
				item.y = item.VO.PosY;
			}
		}
		
		public function Show(delay:Number = 0, duration:Number = 0.5):void{
			var posX:int = (_barWidth)*-1;
			this.x = posX;
			TweenLite.to( this, duration, { x: _marginLeft, autoAlpha:1, delay: delay } );
		}		
		public function Hide():void	{
			var posX:int = (_barWidth )*-1;
			TweenLite.to( this, 0.5, { x: posX, autoAlpha:0, delay: 0 } );
		}
		
		public function Dispose():void{
			_bgBmpData.dispose();
			_bgBmpData = null;
		}
	}
}